<script setup >
import { ref } from 'vue';
import SingleSelect from './SingleSelect.vue';
import Button from './Button_lxw.vue';
import getRequest from '../utils/lxw_request';
import Cookie from 'js-cookie';

const props = defineProps(['title', 'map', 'confirmFunction', 'confirmParams', 'cancelFunction', 'cancelParams']);

const user_info = ref(null)
let edit_info = {};

for(const e of props.map) {
    edit_info[e['key']] = ref("");
}

function getProfiles() {
    const cookie = Cookie.get('userData');
    const userData = cookie == undefined ? null : JSON.parse(cookie);

    getRequest(`/user/home_page/${userData['id']}`, "GET", new Headers({ 'Content-Type': 'application/json' }))
    .then((response) => response.json())
    .then((json) => {
        if (json.code === 200) {
            user_info.value = json.data;
            if(user_info.value['role'] === 5) {
                edit_info['school'].value = user_info.value['school'];
            } else if(user_info.value['role'] === 4) {
                edit_info['school'].value = user_info.value['school'];
                edit_info['college'].value = user_info.value['college'];
            } else if(user_info.value['role'] === 3) {
                edit_info['school'].value = user_info.value['school'];
                edit_info['college'].value = user_info.value['college'];
            } else if(user_info.value['role'] === 2) {
                edit_info['school'].value = user_info.value['school'];
                edit_info['college'].value = user_info.value['college'];
            }
        } else {
            alert(json.msg);
        }
    })
    .catch((err) => {
        console.error(err);
    })

}


function confirm() {
    if(props.confirmFunction == undefined) {
        return ;
    }
    props.confirmParams === undefined ? props.confirmFunction.call({edit_info}) : props.confirmFunction.call({edit_info}, props.confirmParams);
}

function cancel() {
    if(props.cancelFunction === undefined) {
        return ;
    }
    props.cancelParams === undefined ? props.cancelFunction.call({edit_info}) : props.cancelFunction.call({edit_info}, props.cancelParams);
}

getProfiles();
</script>
<template>
    <div class="one-piece-import-form" >
        <div class="title" >{{ props.title }}</div>
        <template v-for="(m, index) in props.map" :key="index" >
                    <div class="input-container" >
                        <div class="item-name" >{{ m['label'] }}</div>
                        <!-- <SingleSelect v-if="m['isEnum']" v-model:selected="edit_info[m['key']]" :options="m['enum']" :disabled="!m['isEdit']" /> -->
                        <input  class="input"  :disabled="!m['isEdit']"  type="text" v-model="edit_info[m['key']].value"  />
                    </div>
        </template>
        <div class="buttons" >
                <Button class="confirm" :style="{margin: '0 15px'}" @click="confirm" >确定</Button>
                <Button class="cancel" :style="{margin: '0 15px'}" @click="cancel" >取消</Button>
            </div>
    </div>
</template>
<style scoped >
.title {
    font-size: 20px;
    margin: 15px auto;
    text-align: center;
    padding: 10px 0;
    border-bottom: 1px solid #C0C4CC;
}

.input-container {
    width: 80%;
    margin: 10px auto;
}

.item-name {
    letter-spacing: 1px;
    font-size: 18px;
}

.item-name::after {
    content: ": "
}

.input {
    width: 100%;
    height: 35px;
    display: inline-block;
    padding: 5px 10px;
    border: none;
    background-color: #f1f1f1;
    letter-spacing: 1px;
    font-size: 16px;
    line-height: 30px;

}

.input:focus {
    outline: none;
    background-color: #C0C4CC;
}

.input.edited {
    background-color:   #f3d19e; 
}

.input.edited:focus {
    outline: none;
    background-color:  #E6A23C;
}

.input:disabled {
    cursor: not-allowed;

}

.buttons {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 15px auto;
}

.confirm {
    width: 120px !important;
    border-color: transparent !important;
    background-color: #8493a6 !important;
    color: #FFFFFF !important;
}

.cancel {
    width: 120px !important;
    border-color: transparent !important;
    background-color: #939393 !important;
    color: #FFFFFF !important;
}

</style>